<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/1/22
 * Time: 10:34
 */

namespace app\api\controller;


use app\admin\model\Config;
use app\common\controller\Base;
use think\Controller;
use think\Db;

class Index extends Controller
{

    /**
     *
     * 首页接口
     *
     * */
    public function index()
    {

        $config = new Config();
        $configInfo = $config->config_cache();
        $lunbo = $configInfo['web_lunbo'];
        $url = $_SERVER["SERVER_NAME"];

        $httpType = is_https();

        if($httpType){
            $http = "https://";
        }else{
            $http = "http://";
        }
        //轮播图
        foreach ($lunbo as $k => $v){
            $lunbo[$k]['path'] = $http.$url."/uploads/".$v['path'];
        }
        //文章列表
        $map[] = ['is_index','=',1];
        $order = "sort desc";
        $horse = "";
        $list = Db::name('article')->where($map)
            ->order($order)
            ->field('title,id')
            ->select();

        $url = $_SERVER['SERVER_NAME'];
        foreach ($list as $k => $v){
            $list[$k]['url'] = $http.$url."/index/index/index?id=".$v['id'];
        }

        $where[] = ['status','=',1];
        $where[] = ['is_index','=',1];
        $article = Db::name('help_article')->where($where)
            ->order('create_time desc')
            ->field('id,title,create_time,pic')
            ->withAttr('create_time', function ($value, $data) {
                if($value){
                    return date('Y-m-d H:i:s', $value);
                }else{
                    return "--";
                }
            })
            ->select();
        foreach ($article as $k => $v){
            if($v['pic']){
                $article[$k]['pic'] = $http.$url."/uploads/images/article/".$v['pic'];
            }
            $article[$k]['url'] = $http.$url."/index/index/dang?id=".$v['id'];
        }

        if(count($lunbo) > 1){
            $data['lunbo'] = array_values($lunbo);
        }else{
            $data['lunbo'] = $lunbo;
        }
        $data['article'] = $article;
        $data['horse'] = $list;


        $this->result($data,'1','获取成功','json',[]);

    }


    /**
     * 爱心苑
     * */
    public function loveList()
    {

        $page = 1;
        $limit = 5;

        $where[] = ['status','in','1,2'];
        $where[] = ['type','=',1];
        $list = Db::name('love_list')->where($where)
            ->order('create_time desc')
            ->page($page,$limit)
            ->field('id,realname,sex,rudang_date,sex,mobile,channel,danwei,info,type,wish_info,status')
            ->withAttr('create_time', function ($value, $data) {
                if($value){
                    return date('Y-m-d H:i:s', $value);
                }else{
                    return "--";
                }
            })
            ->withAttr('wish_info', function ($value, $data) {
                if($value){
                    return $value;
                }else{
                    return "";
                }
            })
            ->select();
        foreach ($list as $k => $v){
            if($v['status'] == 2){
                $list[$k]['is_take'] = 1;
            }else{
                $list[$k]['is_take'] = 0;
            }
        }
        $total = Db::name('love_list')->where($where)->count();

        $data = [
            'list'=>$list,
            'total' =>$total
        ];

        $this->result($data,'1','获取成功','json');

    }


    /**
     *
     * */
    public function secondLoveList()
    {

        $page = input('page');
        $limit = input('limit');
//        halt($limit);
        $where[] = ['status','in','1,2'];
        $where[] = ['type','=',1];
        $list = Db::name('love_list')->where($where)
            ->page($page,$limit)
            ->order('create_time desc')
            ->field('id,realname,sex,rudang_date,sex,mobile,channel,danwei,info,type,wish_info,status')
            ->withAttr('create_time', function ($value, $data) {
                if($value){
                    return date('Y-m-d H:i:s', $value);
                }else{
                    return "--";
                }
            })
            ->withAttr('wish_info', function ($value, $data) {
                if($value){
                    return $value;
                }else{
                    return "";
                }
            })
            ->select();
        foreach ($list as $k => $v){
            if($v['status'] == 2){
                $list[$k]['is_take'] = 1;
            }else{
                $list[$k]['is_take'] = 0;
            }
        }
        $total = Db::name('love_list')->where($where)->count();

        $data = [
            'list'=>$list,
            'total' =>$total
        ];

        $this->result($data,'1','获取成功','json');

    }

    /**
     * 公告列表
     * */
    public function articleList()
    {

        $page = input('page');
        $limit = input('limit');
        $where[] = ['status','=',1];
        $where[] = ['type','=',2];

        $order = "isTop desc && sort desc";

        $list = Db::name('article')
            ->where($where)
            ->page($page,$limit)
            ->order($order)
            ->withAttr('create_time', function ($value, $data) {
                if($value){
                    return date('Y-m-d H:i:s', $value);
                }else{
                    return "--";
                }
            })
            ->field('id,title,pic,create_time,isTop')
            ->select();

        $httpType = is_https();

        if($httpType){
            $https = "https://";
        }else{
            $https = "http://";
        }

        $http = $_SERVER['SERVER_NAME'];
        foreach ($list as $k => $v){
            $list[$k]['url'] = $https.$http."/index/index/index?id=".$v['id'];
        }
        $total = Db::name('article')->where($where)->count();

        $data = [
            'list'=>$list,
            'total' =>$total
        ];

        $this->result($data,'1','获取成功','json');

    }


    /**
     * 帮扶文章中心列表
     * */
    public function helpList()
    {

        $config = new Config();
        $configInfo = $config->config_cache();
        $lunbo = $configInfo['helpLunBo'];

        $page = input('page');
        $limit = input('limit');
        $order = "isTop desc && sort desc";

        $url = $_SERVER["SERVER_NAME"];

        $httpType = is_https();

        if($httpType){
            $http = "https://";
        }else{
            $http = "http://";
        }
        //轮播图
        foreach ($lunbo as $k => $v){
            $lunbo[$k]['path'] = $http.$url."/uploads/".$v['path'];
        }

        $where[] = ['type','=','1'];
        $where[] = ['status','=','1'];
        $list = Db::name('article')
            ->where($where)
            ->page($page,$limit)
            ->field('id,title,pic,create_time,isTop,text')
            ->order($order) ->withAttr('create_time', function ($value, $data) {
                if($value){
                    return date('Y-m-d H:i:s', $value);
                }else{
                    return "--";
                }
            })
            ->select();
        $total = Db::name('article')->where($where)->count();

        if(count($lunbo) > 1){
            $lunbo = array_values($lunbo);
        }

        $data = [
            'list' => $list,
            'total' => $total,
            'lunbo' => $lunbo
        ];

        $this->result($data,'1','获取成功','json');

    }



   /**
     * 就业岗位列表
     * */
    public function work()
    {

        $type = input('type');

        $page = input('page','');
        $limit = input('limit','');
        $token = input('token','');

        if($token){
            $base = new Base();
            $user =  $base->check($token);
            $uid = $user['uid'];
        }else{
            $uid = "";
        }

        $order = "sort desc && create_time desc";

        $imgList = Db::name('work_config')->where(['id' => 1])->find();

        $httpType = is_https();

        if($httpType){
            $https = "https://";
        }else{
            $https = "http://";
        }

        $http = $_SERVER['SERVER_NAME'];

        if($type == 2){
            $where[] = ['type','=',3];
            $where[] = ['status','=','1'];

            $list = Db::name('article')
                ->where($where)
                ->order($order)
                ->page($page,$limit)
                ->withAttr('create_time', function ($value, $data) {
                    if($value){
                        return date('Y-m-d H:i:s', $value);
                    }else{
                        return "--";
                    }
                })
                ->field('id,title,pic,create_time,isTop')
                ->select();

            foreach ($list as $k => $v){
                $list[$k]['url'] = $https.$http."/index/index/index?id=".$v['id'];
            }
            $img = $https.$http."/uploads/images/work/".$imgList['img2'];
            $total = Db::name('article')->where($where)->count();

        }else{

            $where[] = ['type','=',$type];
            $where[] = ['status','=','1'];
            $list = Db::name('work_list')
                ->where($where)
                ->order($order)
                ->page($page,$limit)
                ->withAttr('create_time', function ($value, $data) {
                    if($value){
                        return date('Y-m-d H:i:s', $value);
                    }else{
                        return "--";
                    }
                })
                ->field('id,wages,company,create_time,info,title')
                ->select();

            foreach ($list as $k => $v){
                $list[$k]['url'] = $https.$http."/index/index/work?id=".$v['id']."&uid=".$uid;
            }
            $img = $https.$http."/uploads/images/work/".$imgList['img2'];
            $total = Db::name('work_list')->where($where)->count();

        }
        $data = [
            'list'=>$list,
            'img' => $img,
            'total' =>$total
        ];
        $this->result($data,'1','获取成功','json');

    }

    /**
     * 就业岗位详情
     * */
   public function info()
    {

        $id = input('id');
        $token = input('token');


        $where[] = ['id','=',$id];
        $data = Db::name('work_list')
            ->where($where)
            ->withAttr('create_time', function ($value, $data) {
                if($value){
                    return date('Y-m-d', $value);
                }else{
                    return "--";
                }
            })
            ->find();

        $data['isCollection'] = 0;
        if($token){
            $base = new Base();
            $userInfo = $base->check($token);
            $map[] = ['user_id','=',$userInfo['uid']];
            $map[] = ['work_id','=',$id];
            $count = Db::name('collection_work')->where($map)->count();
            if($count){
                $status = Db::name('collection_work')->where($map)->value('status');
                if($status == 1){
                    $data['isCollection'] = 1;
                }
            }

            $isSq = Db::name('apply_work')->where($map)->count();
            if($isSq == 0){
                $aCount = mb_strlen($data['contacts'],"utf-8");
                $aCount2 = mb_strlen($data['phone'],"utf-8");
                $dCount = mb_strlen($data['address'],"utf-8");
                $data['contacts'] = hidestr($data['contacts'],1,$aCount-2 );
                $data['phone'] = hidestr($data['phone'],2,$aCount2-2);
                $data['address'] = hidestr($data['address'],2,$dCount-2);
            }

        }else{

            $aCount = mb_strlen($data['contacts'],"utf-8");
            $aCount2 = mb_strlen($data['phone'],"utf-8");
            $dCount = mb_strlen($data['address'],"utf-8");
            $data['contacts'] = hidestr($data['contacts'],1,$aCount-2 );
            $data['phone'] = hidestr($data['phone'],2,$aCount2-2);
            $data['address'] = hidestr($data['address'],2,$dCount-2);

        }

        $this->result($data,'1','获取成功','json');

    }


    /**
     * 爱心苑列表详情
     * */
    public function loveInfo()
    {

        $id = input('id','');
        $token = input('token','');

        if(!$id){
            $this->result('','-1','数据获取错误','json');
        }
        $info = Db::name('love_list')->where(['id' => $id])->field('realname,mobile,danwei,info,wish_info,status,help_id,rudang_date,channel,sex,id,type,address,address_info')->find();

        if($info['status'] == 1){
            $info['is_take'] = 1;
        }else{
            $info['is_take'] = 0;
        }


        if($token){
            $base = new Base();
            $user = $base->check($token);
            if($info['help_id'] != $user['uid']){
                $aCount = mb_strlen($info['address'],"utf-8");
                $aCount2 = mb_strlen($info['address_info'],"utf-8");
                $dCount = mb_strlen($info['danwei'],"utf-8");
                $info['mobile'] = hidestr($info['mobile'],3,4);
                $info['danwei'] = hidestr($info['danwei'],2,$dCount-2);
                $info['address'] = hidestr($info['address'],2,$aCount-2);
                $info['address_info'] = hidestr($info['address_info'],0,$aCount2);
                $info['is_mine'] = 0;

                $info['is_show'] = 0;

                $info['config_name'] = '';
                $info['config_mobile'] = '';
                $info['config_address'] = '';
            }else{
                $info['is_mine'] = 1;
                $info['is_show'] = 1;

                $where[] = ['id','in',[19,20,21]];
                $config = Db::name('config')->where($where)->field('value')->select();

                $info['config_name'] = $config[0]['value'];
                $info['config_mobile'] = $config[1]['value'];
                $info['config_address'] = $config[2]['value'];
            }
        }else{
            $aCount = mb_strlen($info['address'],"utf-8");
            $aCount2 = mb_strlen($info['address_info'],"utf-8");
            $dCount = mb_strlen($info['danwei'],"utf-8");
            $info['mobile'] = hidestr($info['mobile'],3,4);
            $info['danwei'] = hidestr($info['danwei'],2,$dCount-2);
            $info['address'] = hidestr($info['address'],2,$aCount-2);
            $info['address_info'] = hidestr($info['address_info'],0,$aCount2);
            $info['is_mine'] = 0;

            $info['is_show'] = 0;

            $info['config_name'] = '';
            $info['config_mobile'] = '';
            $info['config_address'] = '';
        }

        if(empty($info['address'])){
            $info['address'] = "";
        }

        if(empty($info['address_info'])){
            $info['address_info'] = "";
        }


        $this->result($info,'1','获取成功','json');

    }



}